<template>
  <div class="input-container">
    <div class="input-wrapper">
      <div class="input-form">
        <textarea
          v-model="inputMessage"
          @keyup.enter.exact="sendMessage"
          @keydown.enter.exact.prevent
          @input="handleInput"
          rows="1"
          placeholder="发送消息..."
          :disabled="disabled"
          class="input-textarea"
          ref="textarea"
        ></textarea>

        <button
          v-if="!loading"
          @click="sendMessage"
          :disabled="disabled || !inputMessage.trim()"
          class="send-button"
        >
          <svg class="send-icon" viewBox="0 0 24 24" fill="none">
            <path d="M7 11L12 6L17 11M12 18V7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
        <button
          v-else
          @click="stopGeneration"
          class="stop-button"
          title="停止生成"
        >
          <svg viewBox="0 0 24 24" class="stop-icon">
            <path fill="currentColor" d="M6 6h12v12H6z"/>
          </svg>
        </button>
      </div>

      <div class="input-footer">
        <div class="input-tips">
          <span v-if="!isConnected" class="error-tip">
            未连接到服务器
          </span>
          <span v-else class="normal-tip">
            <kbd>Enter</kbd> 发送 &nbsp; <kbd>Shift + Enter</kbd> 换行
          </span>
        </div>
      </div>
    </div>

    <div class="input-bottom-text">
      仅供学习交流使用
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  loading: Boolean,
  disabled: Boolean,
  isConnected: Boolean
})

const emit = defineEmits(['send', 'stop'])

const inputMessage = ref('')
const textarea = ref(null)

const handleInput = () => {
  autoResize()
}

const autoResize = () => {
  const el = textarea.value
  if (el) {
    el.style.height = 'auto'
    el.style.height = el.scrollHeight + 'px'
  }
}

const sendMessage = () => {
  if (!inputMessage.value.trim() || props.disabled) return

  emit('send', inputMessage.value)
  inputMessage.value = ''
  handleInput()
}

const stopGeneration = () => {
  emit('stop')
}

// 监听loading变化,重置输入框高度
watch(() => props.loading, (newVal) => {
  if (!newVal) {
    autoResize()
  }
})
</script>

<style scoped>
.input-container {
  position: relative;
  width: 100%;
  max-width: 48rem;
  margin: 0 auto;
  padding: 8px 16px;
}

.input-wrapper {
  border-radius: 12px;
  border: 1px solid #e5e7eb;
  background-color: #ffffff;
  box-shadow: 0 0 15px rgba(0,0,0,0.05);
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.input-form {
  position: relative;
  display: flex;
  align-items: flex-end;
  padding: 12px;
}

.input-textarea {
  flex: 1;
  min-height: 24px;
  max-height: 200px;
  padding: 8px 12px;
  font-size: 16px;
  line-height: 1.5;
  color: #1f2937;
  background-color: transparent;
  border: none;
  resize: none;
  outline: none;
  overflow-y: auto;
}

.input-textarea::placeholder {
  color: #9ca3af;
}

.send-button {
  position: relative;
  width: 32px;
  height: 32px;
  margin-left: 8px;
  padding: 6px;
  color: #ffffff;
  background-color: #2563eb;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.send-button:hover:not(:disabled) {
  background-color: #1d4ed8;
  transform: translateY(-1px);
}

.send-button:disabled {
  background-color: #e5e7eb;
  cursor: not-allowed;
}

.send-icon {
  width: 20px;
  height: 20px;
}

.input-footer {
  display: flex;
  justify-content: flex-end;
  padding: 8px 12px;
  border-top: 1px solid #f3f4f6;
}

.input-tips {
  font-size: 12px;
  color: #6b7280;
}

.error-tip {
  color: #ef4444;
}

kbd {
  padding: 2px 4px;
  font-size: 11px;
  color: #6b7280;
  background-color: #f3f4f6;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  box-shadow: 0 1px 0 rgba(0,0,0,0.1);
}

.input-bottom-text {
  text-align: center;
  padding: 8px 0;
  font-size: 12px;
  color: #9ca3af;
}

.stop-button {
  position: relative;
  width: 32px;
  height: 32px;
  margin-left: 8px;
  padding: 6px;
  color: #ffffff;
  background-color: #ef4444;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.stop-button:hover {
  background-color: #dc2626;
  transform: translateY(-1px);
}

.stop-button:active {
  transform: translateY(0);
}

.stop-icon {
  width: 20px;
  height: 20px;
}

/* 移动端适配 */
@media (max-width: 640px) {
  .input-container {
    padding: 8px;
  }

  .input-wrapper {
    border-radius: 8px;
  }

  .input-form {
    padding: 8px;
  }

  .input-textarea {
    font-size: 14px;
  }

  .input-footer {
    padding: 6px 8px;
  }
}

/* 暗色主题支持 */
@media (prefers-color-scheme: dark) {
  .input-wrapper {
    background-color: #2d2d2d;
    border-color: #404040;
  }

  .input-textarea {
    color: #e5e7eb;
  }

  .input-textarea::placeholder {
    color: #6b7280;
  }

  .input-footer {
    border-top-color: #404040;
  }

  kbd {
    background-color: #374151;
    border-color: #4b5563;
    color: #e5e7eb;
  }

  .stop-button {
    background-color: #dc2626;
  }

  .stop-button:hover {
    background-color: #b91c1c;
  }
}
</style>
